using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.IO;

using AGG.UI;
using AGG.Image;
using AGG.VertexSource;
using AGG.RasterizerScanline;

namespace AGG
{
    class DemoRunner
    {
        public static List<AppWidget> GetPlugins(string folder)
        {
            //string[] files = Directory.GetFiles(folder, "*_HalFactory.dll");
            string[] files = Directory.GetFiles(folder, "*.dll");

            List<AppWidget> factoryList = new List<AppWidget>();

            foreach (string file in files)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFile(file);

                    foreach (Type type in assembly.GetTypes())
                    {

                        if (!type.IsClass || !type.IsPublic)
                        {
                            continue;
                        }

                        Type[] interfaces = type.GetInterfaces();

                        if (((IList)interfaces).Contains(typeof(AppWidget)))
                        {
                            factoryList.Add((AppWidget)Activator.CreateInstance(type));
                        }
                    }
                }
                catch (BadImageFormatException)
                {
                }
            }

            return factoryList;

        }

        public static void StartDemo()
        {
            AppWidget appWidget = new lion();
            //AppWidget appWidget = new aa_demo();

            GuiHalFactory.SetGuiBackend(GuiHalFactory.KnownGuiFactoriesIndexes.WindowsFormsBitmap);
            GuiHalSurface primaryWindow = GuiHalFactory.CreatePrimarySurface(appWidget.DefaultWidth, appWidget.DefaultHeight, GuiHalSurface.CreateFlags.Resizable, GuiHalSurface.PixelFormat.PixelFormatBgra32);

            primaryWindow.Caption = appWidget.DefaultCaption;

            primaryWindow.AddChild(appWidget);
            primaryWindow.Run();
        }

        [STAThread]
        public static void Main(string[] args)
        {
            StartDemo();
        }
    }
}
